local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerObject());
local ConnectClass=_G.CommonOHOH.load.Connect();
local EventList = ServerOHOH.load.EventList();
local ServerEventManager = ServerOHOH.load.ServerEventManager();


function M.new(self)
	return M(self);
end;
function M.ctor(self, playerInst)

	print("AAAAAAAAAAAA---1--->")
	_G.CommonOHOH.load.PlayerTool().CreateConnectEvent(self)
	M.super.ctor(self, playerInst);
	print("AAAAAAAAAAAA---1.5--->")

end;

function M.dtor(self)
	M.super.dtor(self);
end;

--已有数据，但是客户端player还未设置数据
function M.OnLogin(self)
	print("AAAAAAAAAAAA---2--->")

	if ServerOHOH.load.RBLX_TEST().isTest then
		self.debugTest_comp=self:AddComponent(ServerOHOH.load.Sever_DebugTest());
	end
	
	self.DataBase= self:GetComponent(ServerOHOH.load.Server_PlayerDataComponent());
	self:AddComponent(_G.CommonOHOH.load.Common_GamePassCtrl()); 
	self:AddComponent(ServerOHOH.load.Server_ItemCtrl())
end;


function M.SynchDataToClient(self,...)
	local keys={...}
	local netData={}
	for _,v in pairs(keys) do
		self.UserDataEvent:Action(v)
		local data=self.DataBase:GetData(v)
		assert(data~=nil,"Data==nil,Error Key="..v)
		if type(data)=="table" then
			netData[v]=data.__real
		else
			netData[v]=data
		end
	end
	ServerEventManager:Fire(self, EventList.synch_userdata, netData);
end


function M.FireMsgDataToClient(self,...)
	local keys={...}
	local netData={}
	for _,v in pairs(keys) do
		local data=self.DataBase:GetData(v)
		assert(data~=nil,"Data==nil,Error Key="..v)
		if type(data)=="table" then
			netData[v]=data.__real
		else
			netData[v]=data
		end
	end
	ServerEventManager:Fire(self, EventList.synch_userdata, netData);
end

--客户端已有数据
function M.OnLoginOver(self)
	print("AAAAAAAAAAAA---3--->")

	self:AddComponent(ServerOHOH.load.Server_ThePlayerRankComponent()) --排行榜
	

	_G.CommonOHOH.load.PlayerTool().AddCommonComponent(self)
	print("AAAAAAAAAAAA---4--->")
end;




function M.OnCharacterIn(self, Character)
	self.CharacterEnterEvent:Action(Character)

end;

function M.OnCharacterOut(self, Character)

end;



local TeleportService = game:GetService("TeleportService")

function M.IdledOutTimeTeleport(self,workState)
	self:AddProcess(function()
		local attemptIndex = 0
		local success, result -- define pcall results outside of loop so results can be reported later on	
		local teleportOptions = Instance.new("TeleportOptions")
		local teleportData = {
			placeId = game.PlaceId,
			jobId = game.JobId,
			work=workState
		}
		teleportOptions:SetTeleportData(teleportData)
		repeat
			success, result = pcall(function()
				return TeleportService:TeleportAsync(game.PlaceId,{self.main_inst},teleportOptions) -- teleport the user in a protected call to prevent erroring
			end)
			attemptIndex += 1
			if not success then
				self:Wait(1)
			end
			warn("TeleportTeleportTeleportTeleport-->TT",success,result)
		until success or attemptIndex == 5 -- stop trying to teleport if call was successful, or if retry limit has been reached
		warn("TeleportTeleportTeleportTeleport-->C")
		if not success then
			warn(result) -- print the failure reason to output
		end
	end)
end

ServerOHOH.load.ServerFunctionManager():SetHandle(ServerOHOH.load.FunctionList().create_character, function(player)
	player:CreateCharacter();
end);


ServerOHOH.event_manager:RegistHandle(ServerOHOH.event_list.UpdDone, function(player)
	assert(player,"player==nil")
    local DataBase=player:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())
	DataBase:SetData("upd",_G.CommonOHOH.config.Game.Upd.id)
end);

return M;
